import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;
 
/**
 * @author https://www.wdbyte.com
 */
public class HttpClientDemo {
 
    public static void main(String[] args) {
        // String result = get("http://httpbin.org/get");
        // String result = post("https://iot.chinadre.com/Version/version_data_list");
        String result = post("http://localhost:8080/dataItem/defaultDataItemList");
        System.out.println(result);
    }
 
    public static String post(String url) {
        String resultContent = null;
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> parms = new ArrayList<>();
        parms.add(new BasicNameValuePair("page", "1"));
        parms.add(new BasicNameValuePair("limit", "3"));
        httpPost.setEntity(new UrlEncodedFormEntity(parms));
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
                // 获取状态码
                System.out.println(response.getVersion()); // HTTP/1.1
                System.out.println(response.getCode()); // 200
                System.out.println(response.getReasonPhrase()); // OK
                HttpEntity entity = response.getEntity();
                // 获取响应信息
                resultContent = EntityUtils.toString(entity);
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        return resultContent;
    }
 
}